package com.wangp.employee.service.impl;
import java.math.BigDecimal;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.wangp.employee.dto.DevelopmentQueryReqDTO;
import com.wangp.employee.dto.DevelopmentQueryRespDTO;
import com.wangp.employee.dto.DevelopmentSaveReqDTO;

import com.wangp.employee.dto.EmployeeDTO;
import com.wangp.employee.mapper.DevelopmentMapper;
import com.wangp.employee.mapper.EmployeeMapper;
import com.wangp.employee.po.DeveAndEmpDO;
import com.wangp.employee.po.DevelopmentDO;
import com.wangp.employee.po.EmployeeDO;
import com.wangp.employee.service.DevelopmentService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 第一行简要描述.
 * </p>
 *
 * @author wangping
 * @since 2024-12-19
 */
@Service
@Slf4j
public class DevelopmentServiceImpl implements DevelopmentService {

    @Autowired
    private DevelopmentMapper developmentMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public DevelopmentQueryRespDTO queryPage(final DevelopmentQueryReqDTO developmentDTO) {
        //分页
        int page = developmentDTO.getPage();
        int size = developmentDTO.getSize();
        int offset = (page - 1) * size;

        // 分页数据
        List<DeveAndEmpDO> list= developmentMapper.findDevelopmentPageData(offset, size);

        // 查询总记录数
        int count = developmentMapper.findDevelopmentPageDataTotal();

        // 封装分页数据
        DevelopmentQueryRespDTO result = new DevelopmentQueryRespDTO();
        result.setData(list);
        result.setPage(page);
        result.setSize(size);
        result.setTotal(count);
        return result;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDepartmentWithEmp(final DevelopmentSaveReqDTO saveReqDTO) {
        log.info(JSONObject.toJSONString(saveReqDTO, true));
        DevelopmentDO developmentDO = new DevelopmentDO();
        BeanUtils.copyProperties(saveReqDTO, developmentDO);


        developmentMapper.insertDepartmentWithEmp(developmentDO);



        List<EmployeeDTO> employeeDTOList = saveReqDTO.getEmployeeDTOArrayList();


        employeeDTOList.forEach(employeeDTO->{
            employeeDTO.setDepartmentId(developmentDO.getId());
        });
        log.info(JSONObject.toJSONString(employeeDTOList, true));
        employeeMapper.insertBatchEmps(employeeDTOList);

    }




    private List<EmployeeDO> convertToEmpDOList(final List<EmployeeDTO> employeeDTOList) {

        List<EmployeeDO> employeeDOS = new ArrayList<>();

        for (final EmployeeDTO employeeDTO : employeeDTOList) {
            EmployeeDO employeeDO=  convertToEmployeeDO(employeeDTO);
            employeeDOS.add(employeeDO);
        }
        return employeeDOS;

    }

    private EmployeeDO convertToEmployeeDO(final EmployeeDTO employeeDTO) {
        if (employeeDTO == null) {
            return null;
        }
        EmployeeDO employeeDO = new EmployeeDO();
        employeeDO.setEmployeeName(employeeDTO.getEmployeeName());
        employeeDO.setEmployeeAge(employeeDTO.getEmployeeAge());
        employeeDO.setEmployeeGender(employeeDTO.getEmployeeGender());
        employeeDO.setJoinDate(employeeDTO.getJoinDate());
        employeeDO.setSalary(employeeDTO.getSalary());
        employeeDO.setDepartmentId(employeeDTO.getDepartmentId());
        employeeDO.setCreateDate(employeeDTO.getCreateDate());
        employeeDO.setCreateBy(employeeDTO.getCreateBy());
        employeeDO.setUpdateDate(employeeDTO.getUpdateDate());
        employeeDO.setUpdateBy(employeeDTO.getUpdateBy());
        employeeDO.setIsDeleted("0");
        return employeeDO;
    }

}
